package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
@Slf4j
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        log.debug("Controller: add: checkGroup ===> " + checkGroup);
        log.debug("Controller: add: checkItemIds ===> " + checkItemIds);
        checkGroupService.add(checkGroup,checkItemIds);
        log.debug("新增检查组成功");
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("Controller: findByPage: queryPageBean ===> " + queryPageBean);
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        log.debug("Controller: findByPage: pageResult ===> " + pageResult);
        log.debug("分页查询检查组成功");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("Controller: delById: id ===> " + id);
        checkGroupService.delById(id);
        log.debug("删除检查组成功");
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("Controller: findById: id ===> " + id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        log.debug("Controller: findById: checkGroup ===> " + checkGroup);
        log.debug("根据id查询检查组成功");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @RequestMapping("/findCheckItemIdsById")
    public Result findCheckItemIdsById(Integer id){
        log.debug("Controller: findById: id ===> " + id);
        List<Integer> checkItemIdsList = checkGroupService.findCheckItemIdsById(id);
        log.debug("Controller: findById: checkItemIdsList ===> " + checkItemIdsList);
        log.debug("查询检查组关联检查项成功");
        return new Result(true,MessageConst.ACTION_SUCCESS,checkItemIdsList);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        log.debug("Controller: edit: checkGroup ===> " + checkGroup);
        log.debug("Controller: edit: checkItemIds ===> " + checkItemIds);
        checkGroupService.edit(checkGroup,checkItemIds);
        log.debug("编辑检查组成功");
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("Controller: findAll: ===> ");
        List<CheckGroup>checkGroupList = checkGroupService.findAll();
        log.debug("Controller: findAll: checkGroupList ===> " + checkGroupList);
        log.debug("查询所有检查组成功");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }

}
